package sdk.com.google.protobuf.demo1;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.titan.core.utils.debug.Timer;

public class Demo1 {

    private static void run() throws Exception {
        // 序列化
//        writeObj();
        // 反向序列化
//        readObj();
        
        
    } 

    private static void writeObj() throws Exception {
        PersonProto.Person person = PersonProto.Person.newBuilder()
                .setId(1).setEmail("a.163.com").setName("小明")
                .build();
        person.writeTo(new FileOutputStream("E:/prototbuf.data"));
    }

    private static void readObj() throws Exception {
        PersonProto.Person person = PersonProto.Person.parseFrom(new FileInputStream("E:/prototbuf.data"));
        System.out.println(String.format("id=%s  name=%s  email=%s"
                , person.getId(), person.getName(), person.getEmail()
                ));
    }    

    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print();
        }
    }

}
